﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Reflection;

namespace FileMonitor.Service.Provider.FileMonitor
{

     //<add name="IPPDMRP007" DataFilter="" Filter="*.txt" SplitArr="" BizProcesses="" TargetDatabaseName="DefaultExchangeDB" 
    //SplitChar="^" TargetTable="SAPForecastT" PlantCodeIndex="0"
     //      BackUpPath="D:\SICT\Files\IPPDL10MRP007\backup" MonitorPath="D:\SICT\Files\IPPDL10MRP007"
     //      typeName="TXTMonitor" WaitTime="5" PlantCodeLength="4" LogPath="D:\SICT\Files\IPPDL10MRP007\Log" DataType="IPPD015FKS" />

  
    public class Configuration
    {
        private static Dictionary<string, PropertyInfo> propertyInfoCache=new Dictionary<string, PropertyInfo>();
        private static object lockObj = new object();

        public string Name { get;  set; }

        public string DataType { get;  set; }

        public string DataFilter { get;  set; }

        public string SplitArr { get;  set; }

        public string BizProcesses { get;  set; }

        public string TargetDatabaseName { get;  set; }

        public string SplitChar { get;  set; }

        public string TargetTable { get;  set; }

        public string PlantCodeIndex { get;  set; }

        public string BackUpPath { get;  set; }

        public string MonitorPath { get;  set; }

        public string typeName { get;  set; }

        public string WaitTime { get;  set; }

        public string PlantCodeLength { get; set; }

        public string LogPath { get; set; }

        public string FilterPlantCode { get; set; }

        public void Initialize(NameValueCollection allConfigs)
        {
            foreach (string name in allConfigs.AllKeys)
            {
                PropertyInfo pi = GetPropertyInfo(name);
                if (pi != null)
                {
                    pi.SetValue(this, allConfigs[name], null);
                }

            }
        }

        private PropertyInfo GetPropertyInfo(string name)
        {
            try
            {
                if (!Configuration.propertyInfoCache.ContainsKey(name))
                {
                    lock (lockObj)
                    {
                        if (!Configuration.propertyInfoCache.ContainsKey(name))
                        {
                            PropertyInfo pi = this.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                            Configuration.propertyInfoCache.Add(name, pi);
                        }
                    }
                }
                return Configuration.propertyInfoCache[name];
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
